<!--
 * @Descripttion: 
 * @version: 
 * @Author: 夜殇
 * @Date: 2020-07-06 13:22:23
 * @LastEditors: 夜殇
 * @LastEditTime: 2020-07-07 10:15:02
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>类名操作</title>
    <style>
      .active {
        width: 100%;
        height: 100px;
        background-color: aqua;
      }
    </style>
  </head>
  <body>
    <button class="add">添加类名</button>
    <button class="remove">移除类名</button>
    <button class="toggle">切换类名</button>
    <button class="contains">判断当前元素是否存在某类名</button>
    <div></div>
    <script>
      const div = document.querySelector('div')
      const add = document.querySelector('.add')
      const remove = document.querySelector('.remove')
      const toggle = document.querySelector('.toggle')
      const contains = document.querySelector('.contains')

      add.addEventListener('click', e => {
        div.classList.add('active')
      })

      remove.addEventListener('click', e => {
        div.classList.remove('active')
      })

      toggle.addEventListener('click', e => {
        div.classList.toggle('active')
      })

      contains.addEventListener('click', e => {
        if (div.classList.contains('active')) {
          div.classList.remove('active')
        } else {
          div.classList.add('active')
        }
      })

      console.log(div, add, remove, toggle, contains)
    </script>
  </body>
</html>
